<?php

include DIRETORIO."app/Models/MeetingDAO.php";

class C_Meeting{

	protected static $lk = NULL;
	
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}

/******************************************************************************************************************
Funções das Reuniões
******************************************************************************************************************/
	public static function getMeetingTypes(){
		$return = null;

		$objMeetingDAO	 = new MeetingDAO(self::getDB());
		$objMettingTypes = $objMeetingDAO->getMeetingTypes();	
		
		if(count($objMettingTypes) > 0){
			$html = '<ul>';

			foreach($objMettingTypes as $row){
				$html .= self::buildMeetingType($row);
			}

			$html .= '</ul>';
			$return[] = true;
		}else{
			$html = '<ul><li><h2>Nenhum Tipo de Reunião encontrado!</h2></li></ul>';
			$return[] = false;
		}
	
		$return[] = $html;	
		return json_encode($return);
	}

	public static function buildMeetingType($meetingType){
		$html	= '	<li>
						<div class="box_kanban" id="meetingType_'.$meetingType->id.'">
							<span class="title_bar">
								<span>'.$meetingType->name.'</span>
							</span>
							<span class="title">Descrição<button class="expand">+</button>
							</span>
							<div class="details">
								<span>'.$meetingType->description.'</span>
							</div>
						</div>
					</li>';

		
		return $html;
	}

	public static function getMeetings($post){
		$return = null;
		$idSprint		 = (int)$post["idSprint"];
		$objMeetingDAO	 = new MeetingDAO(self::getDB());
		$objMettingTypes = $objMeetingDAO->getMeetings(0,$idSprint);	
		
		if(count($objMettingTypes) > 0){
			$html = '<ul>';

			foreach($objMettingTypes as $row){
				$html .= self::buildMeeting($row);
			}

			$html .= '</ul>';
			$return[] = true;
		}else{
			$html = '<ul><li><h2>Nenhum registro encontrado!</h2></li></ul>';
			$return[] = false;
		}
	
		$return[] = $html;	
		return json_encode($return);
	}

	public static function buildMeeting($meeting){
		if($meeting->idMeetingType == 2){
			$buttons = '<a onclick="usersMeeting('.$meeting->id.')" href="javascript:void(0);" class="icon meeting"><span>editar</span></a>';
		}
		
		$tempDate	 = explode(" - ",$meeting->dateInit);
		$dateMeeting = $tempDate[0];
		$hourInit	 = $tempDate[1];

		$tempDate	 = explode(" - ",$meeting->dateEnd);
		$hourEnd	 = $tempDate[1];

		$description = $meeting->description;

		if(strlen($description) > 100){
			$description = substr($description, 0, 100)." ...";
		}

		$html	= '	<li>
						<div class="box_kanban" id="meeting_'.$meeting->id.'">
							<span class="title_bar">
								<span>'.$meeting->abstract.'</span>
							</span>
							<span class="title">Tipo: '.$meeting->meetingType.'<button class="expand">+</button>
							</span>
							<div class="details">
								<span>
								Data: '.$dateMeeting.'<br>
								Hora Inicial: '.$hourInit.'<br>
								Hora Final: '.$hourEnd.'<br>
								Descrição: '.$description.'<br>
								</span>
							</div>
							<div class="buttons">
								'.$buttons.'
								<a onclick="editMeeting('.$meeting->id.','.$meeting->idSprint.')" href="javascript:void(0);" class="icon edit"><span>editar</span></a>
								<a onclick="deleteMeeting('.$meeting->id.')" href="javascript:void(0);" class="icon delete"><span>deletar</span></a>
							</div>				
						</div>
					</li>';

		
		return $html;
	}

	public static function newMeeting($post){
		$return = null;
		$html	= "";

		$idSprint		= (int)$post['idSprint'];
		$idMeetingType	= (int)$post['idMeetingType'];
		
		if($idSprint > 0 && $idMeetingType > 0){			
			$objSprintDAO	= new SprintDAO(self::getDB());
			$objSprint		= $objSprintDAO->getSprints($idSprint,$_SESSION["PROJECT"]->id);

			$objMeetingDAO	= new MeetingDAO(self::getDB());		
			$objMeetingType	= $objMeetingDAO->getMeetingTypes($idMeetingType);

		
			$html = file_get_contents(TEMPLATES."meeting.html");
			
			$html = str_replace("[TITLE]","Nova Reunião",$html);
			$html = str_replace("[ID_MEETING]",0,$html);
			$html = str_replace("[ID_MEETING_TYPE]",$idMeetingType,$html);
			$html = str_replace("[ID_SPRINT]",$idSprint,$html);
			$html = str_replace("[SPRINT]","Sprint ".$objSprint[0]->order,$html);
			$html = str_replace("[MEETING_TYPE]",$objMeetingType[0]->name,$html);
			$html = str_replace("[ABSTRACT]","",$html);
			$html = str_replace("[DESCRIPTION]","",$html);
			$html = str_replace("[DATE]",date("d/m/Y"),$html);
			$html = str_replace("[HOUR_INIT]","",$html);
			$html = str_replace("[HOUR_END]","",$html);
		}

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível buscar a Reunião!");
		}

		return json_encode($return);
	}

	public static function editMeeting($post){
		$return = null;
		$html	= "";

		$idSprint	= (int)$post['idSprint'];
		$idMeeting	= (int)$post['idMeeting'];
		
		if($idSprint > 0 && $idMeeting > 0){			
			$objSprintDAO	= new SprintDAO(self::getDB());
			$objSprint		= $objSprintDAO->getSprints($idSprint,$_SESSION["PROJECT"]->id);

			$objMeetingDAO	= new MeetingDAO(self::getDB());		
			$objMeeting		= $objMeetingDAO->getMeetings($idMeeting,$idSprint);

			$dateInit = explode(" - ",$objMeeting[0]->dateInit);
			$dateEnd  = explode(" - ",$objMeeting[0]->dateEnd);

			$html = file_get_contents(TEMPLATES."meeting.html");			
			$html = str_replace("[TITLE]","Editar Reunião",$html);
			$html = str_replace("[ID_MEETING]",$objMeeting[0]->id,$html);
			$html = str_replace("[ID_MEETING_TYPE]",$objMeeting[0]->idMeetingType,$html);
			$html = str_replace("[ID_SPRINT]",$objMeeting[0]->idSprint,$html);
			$html = str_replace("[SPRINT]","Sprint ".$objSprint[0]->order,$html);
			$html = str_replace("[MEETING_TYPE]",$objMeeting[0]->meetingType,$html);
			$html = str_replace("[ABSTRACT]",$objMeeting[0]->abstract,$html);
			$html = str_replace("[DESCRIPTION]",$objMeeting[0]->description,$html);
			$html = str_replace("[DATE]",$dateInit[0],$html);
			$html = str_replace("[HOUR_INIT]",$dateInit[1],$html);
			$html = str_replace("[HOUR_END]",$dateEnd[1],$html);
		}

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível buscar a Reunião!");
		}

		return json_encode($return);
	}		

	public static function saveMeeting($post){
		$return = null;
		$id		= 0;
		
		if(count($post) > 0){
			$objMeetingDAO	= new MeetingDAO(self::getDB());
			$objMeeting		= new Meeting();

			foreach($post as $key => $value){
				if(stristr($key,"date") || stristr($key,"hour")){
					continue;				
				}
				$objMeeting->$key = $value;
			}
			
			$tempDate	= explode("/",$post['date']);
			$date		= $tempDate[2]."-".$tempDate[1]."-".$tempDate[0];

			$objMeeting->dateInit = $date." ".$post['hourInit'];
			$objMeeting->dateEnd  = $date." ".$post['hourEnd'];
						
			if($objMeeting->id > 0){		
				$objMeetingDAO->save($objMeeting);
				$id = $objMeeting->id;
			}else{
				$objMeeting->idUserCreator	= $_SESSION["USER"]->id;
				$objMeeting->status			= 1; //Ativa
				$id = $objMeetingDAO->save($objMeeting);
			}
		}
		

		if($id > 0){
			$return =  array(true,"Reunião \"<i>". $objMeeting->abstract ."</i>\" salva com sucesso.",$id);
		}else{
			$return =  array(false,"Não foi possível salvar a Reunião!");
		}


		return json_encode($return);
	}

	public static function deleteMeeting($post){
		$objMeetingDAO	= new MeetingDAO(self::getDB());
		$idMeeting		= (int)$post['idMeeting'];
		$result			= 0;

		if($idMeeting > 0)
			$result = $objMeetingDAO->deleteMeeting($idMeeting);

		if($result > 0)
			$return = array(true,"Reunião excluída com sucesso!");
		else
			$return = array(false,"Erro, não foi possível excluir a Reunião!");

		return json_encode($return);
	}


/******************************************************************************************************************
Funções do Calendario
******************************************************************************************************************/

	public static function getMeetingsCalendar($post){
		$return = null;

		$objMeetingDAO	= new MeetingDAO(self::getDB());
		$objMeetings	= $objMeetingDAO->getMeetingsCalendar($_SESSION["PROJECT"]->id);
		
		if(count($objMeetings) > 0){
			$html = self::objToEvent($objMeetings);
			$return[] = true;
		}else{
			$html = '';
			$return[] = false;
		}		
	
		$return[] = $html;
		return json_encode($return);
	}

	public static function objToEvent($obj){
		$tempArray;
		$i = 0;

		foreach($obj as $row){
			$dateInit	= new DateTime($row->dateInit);
			$dateEnd	= new DateTime($row->dateEnd);

			$date		= $dateInit->format("d/m/Y");
			$hourInit	= $dateInit->format("H:i");
			$hourEnd	= $dateEnd->format("H:i");

			$tempArray[$i] = array(	'id'			=> $row->id,
									'idSprint'		=> $row->idSprint,
									'title'			=> $row->abstract,
									'description'	=> $row->description,
									'meetingType'	=> $row->meetingType,
									'dateEvent'		=> $date,
									'hourInit'		=> $hourInit,
									'hourEnd'		=> $hourEnd,
									'nameCriator'	=> $row->nameCriator,
									'start'			=> $row->dateInit,
									'end'			=> $row->dateEnd,
									'editable'		=> false,
									'allDay'		=> false,
									'backgroundColor' => "#008442",
									'borderColor' => "#008442"
									);
			$i++;
		}
		return $tempArray;
	}	

/******************************************************************************************************************
Funções dos Sprints para as reuniões
******************************************************************************************************************/
	public static function getSprints($post){
		$return = null;

		$objMeetingDAO	= new MeetingDAO(self::getDB());
		$objSprints		= $objMeetingDAO->getSprints(0, $_SESSION["PROJECT"]->id);

		if(count($objSprints) > 0){
			$html = '<ul class="sprints">';

			foreach($objSprints as $row){
				$html .= '	<li id="sprint_'.$row->id.'">
								<div class="description">
									<span class="title">Sprint '.$row->order.'</span>
									<span><strong class="countMeetings">'.$row->meetings.'</strong> Reunião(ões)</span>
									<span>Data de Início: '.$row->dateBegin.'</span>
								</div>
								<div class="buttons">
									<a href="'.SITE.'content/kanban/index.php?id='.$row->id.'" class="icon kanban"><span>kanban</span></a>
									<a onclick="loadMeetings('.$row->id.')" href="javascript:void(0);" class="icon tasks"><span>tarefas</span></a>
								</div>								
							</li>';
			}
			
			$html .= '</ul>';
			$return[] = true;
		}else{
			$html = "<h2>Nenhum registro encontrado!</h2>";
			$return[] = false;
		}
	
		$return[] = $html;	
		return json_encode($return);
	}

	public static function getMeetingSprints(){
		$return = array(false,"Não foi possível atualizar os Sprints!");

		$objMeetingDAO = new MeetingDAO(self::getDB());
		$objSprints = $objMeetingDAO->getSprints(0,$_SESSION["PROJECT"]->id);

		if(count($objSprints) > 0){
			foreach($objSprints as $row){
				$result[] = array("id" => $row->id, "meetings" => $row->meetings);
			}

			$return = array(true,$result);
		}

		return json_encode($return);
	}

/******************************************************************************************************************
Funções de Detalhamento por Usuário
******************************************************************************************************************/
	public static function newUserMeeting($post){
		$return = null;
		$html	= "";

		$idMeeting = (int)$post['idMeeting'];
		
		if($idMeeting > 0){						
			$objMeetingDAO	= new MeetingDAO(self::getDB());		
			$objMeeting		= $objMeetingDAO->getMeetings($idMeeting,0);

			$objSprintDAO	= new SprintDAO(self::getDB());
			$objSprint		= $objSprintDAO->getSprints($objMeeting[0]->idSprint,$_SESSION["PROJECT"]->id);

			$objUserDAO		= new UserDAO(self::getDB());		
			$objUsers		= $objUserDAO->getUsersTeam(0,$objSprint[0]->idTeam);

			$selectUsers = "";
			foreach($objUsers as $row){
				$selectUsers .= '<option value="'.$row->id.'">'.$row->name.'</option>';
			}

			$html = file_get_contents(TEMPLATES."userMeeting.html");			
			$html = str_replace("[TITLE]","Detalhamento por Usuário",$html);
			$html = str_replace("[ID_USER_MEETING]",0,$html);
			$html = str_replace("[ID_MEETING]",$idMeeting,$html);
			$html = str_replace("[MEETING]",$objMeeting[0]->abstract,$html);
			$html = str_replace("[LIST_USERS]",$selectUsers ,$html);
		}

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível buscar os Detalhamentos por Usuário!");
		}

		return json_encode($return);
	}

	public static function getUserMeeting($post){
		$return		= array(false, "Não foi possível buscar as informações do usuário!");
		$idMeeting	= (int)$post["idMeeting"];
		$idUser		= (int)$post["idUser"];
		
		if($idMeeting > 0 && $idUser > 0){
			$objMeetingDAO	= new MeetingDAO(self::getDB());
			$objUserMetting = $objMeetingDAO->getUserMeeting($idMeeting,$idUser);
			
			if(count($objUserMetting) > 0){
				$return = array(true, $objUserMetting[0]->getParamArray());
			}else{
				$return = array(false, "");
			}
		}

		return json_encode($return);
	}

	public static function saveUserMeeting($post){
		$return = null;
		$id		= 0;
		
		if(count($post) > 0){
			$objMeetingDAO	= new MeetingDAO(self::getDB());
			$objUserMeeting	= new UserMeeting();

			foreach($post as $key => $value){
				$objUserMeeting->$key = $value;
			}		
						
			if($objUserMeeting->id > 0){		
				$objMeetingDAO->saveUserMeeting($objUserMeeting);
				$id = $objUserMeeting->id;
			}else{
				$id = $objMeetingDAO->saveUserMeeting($objUserMeeting);
			}
		}
		
		if($id > 0){
			$return =  array(true,"<i>Detalhamento do usuário salvo com sucesso.");
		}else{
			$return =  array(false,"Não foi possível salvar o Detalhamento do usuário!");
		}

		return json_encode($return);
	}
}
?>